package com.wwtech.epp.ui.activity.friend.presenter;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.support.annotation.NonNull;

import com.socks.library.KLog;
import com.wwtech.epp.R;
import com.wwtech.epp.contain.ContainValue;
import com.wwtech.epp.data.api.HttpAPIWrapper;
import com.wwtech.epp.entity.BaseBack;
import com.wwtech.epp.entity.GroupInfo;
import com.wwtech.epp.entity.NodeMember;
import com.wwtech.epp.entity.eventbus.ModifyGroupInfo;
import com.wwtech.epp.ui.activity.friend.contract.GroupInfoContract;
import com.wwtech.epp.ui.activity.friend.GroupInfoActivity;
import com.wwtech.epp.ui.activity.work.EditTextActivity;

import java.util.Map;

import javax.inject.Inject;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Action;
import io.reactivex.functions.Consumer;
import io.rong.eventbus.EventBus;
import io.rong.imkit.RongIM;
import io.rong.imlib.model.Group;

/**
 * @author hzp
 * @Package com.wwtech.epp.ui.activity.friend
 * @Description: presenter of GroupInfoActivity
 * @date 2017/12/06 11:49:09
 */
public class GroupInfoPresenter implements GroupInfoContract.GroupInfoContractPresenter{

    HttpAPIWrapper httpAPIWrapper;
    private final GroupInfoContract.View mView;
    private CompositeDisposable mCompositeDisposable;
    private GroupInfoActivity mActivity;
    private GroupInfo mGroupInfo;
    private boolean isCheck = false;
    @Inject
    public GroupInfoPresenter(@NonNull HttpAPIWrapper httpAPIWrapper, @NonNull GroupInfoContract.View view, GroupInfoActivity activity) {
        mView = view;
        this.httpAPIWrapper = httpAPIWrapper;
        mCompositeDisposable = new CompositeDisposable();
        this.mActivity = activity;
    }
    @Override
    public void subscribe() {

    }

    @Override
    public void unsubscribe() {
        if (!mCompositeDisposable.isDisposed()) {
             mCompositeDisposable.dispose();
        }
    }

    @Override
    public void getGroupInfo(String groupId, Map map) {
        mView.showProgressDialog();
        Disposable disposable = httpAPIWrapper.getGroupInfo(groupId, map)
                .subscribe(new Consumer<GroupInfo>() {
                    @Override
                    public void accept(GroupInfo groupInfo) throws Exception {
                        //isSuccesse
                        KLog.i("onSuccesse");
                        mGroupInfo = groupInfo;
                        mView.closeProgressDialog();
                        mView.setGroupInfo(groupInfo);
                        ContainValue.currentGroupMember = groupInfo.getData().getGroupUsersProfile();
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        //onError
                        KLog.i("onError");
                        mView.closeProgressDialog();
                        throwable.printStackTrace();
                    }
                }, new Action() {
                    @Override
                    public void run() throws Exception {
                        //onComplete
                        KLog.i("onComplete");
                        mView.closeProgressDialog();
                    }
                });
        mCompositeDisposable.add(disposable);
    }

    @Override
    public void editGroupName() {
        Intent intent = new Intent((Activity)mView, EditTextActivity.class);
        intent.putExtra("title", "修改群名称");
        intent.putExtra("content", mGroupInfo.getData().getGroupName());
        ((Activity)mView).startActivityForResult(intent, 0);
        ((Activity)mView).overridePendingTransition(R.anim.activity_translate_in, R.anim.activity_translate_out);
    }

    @Override
    public void editGroupInfo(Map map) {
        mView.showProgressDialog();
        isCheck = true;
        Disposable disposable = httpAPIWrapper.modifyGroup(map)
                .subscribe(new Consumer<BaseBack>() {
                    @Override
                    public void accept(BaseBack baseBack) throws Exception {
                        //isSuccesse
                        KLog.i("onSuccesse");
                        mView.closeProgressDialog();
                        mView.modifySuccess();
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        //onError
                        KLog.i("onError");
                        mView.closeProgressDialog();
                        throwable.printStackTrace();
                    }
                }, new Action() {
                    @Override
                    public void run() throws Exception {
                        //onComplete
                        KLog.i("onComplete");
                        mView.closeProgressDialog();
                    }
                });
        mCompositeDisposable.add(disposable);
    }

    @Override
    public void addGroupMember(String groupId, Map map) {
        mView.showProgressDialog();
        Disposable disposable = httpAPIWrapper.addGroupMember(groupId, map)
                .subscribe(new Consumer<BaseBack>() {
                    @Override
                    public void accept(BaseBack baseBack) throws Exception {
                        //isSuccesse
                        KLog.i("onSuccesse");
                        mView.closeProgressDialog();
                        mView.modifySuccess();
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        //onError
                        KLog.i("onError");
                        mView.closeProgressDialog();
                        throwable.printStackTrace();
                    }
                }, new Action() {
                    @Override
                    public void run() throws Exception {
                        //onComplete
                        KLog.i("onComplete");
                        mView.closeProgressDialog();
                    }
                });
        mCompositeDisposable.add(disposable);
    }

    @Override
    public void deldeteGroupMember(String groupId, Map map) {
        mView.showProgressDialog();
        Disposable disposable = httpAPIWrapper.deleteGroupMember(groupId, map)
                .subscribe(new Consumer<BaseBack>() {
                    @Override
                    public void accept(BaseBack baseBack) throws Exception {
                        //isSuccesse
                        KLog.i("onSuccesse");
                        mView.closeProgressDialog();
                        mView.modifySuccess();
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        //onError
                        KLog.i("onError");
                        mView.closeProgressDialog();
                        throwable.printStackTrace();
                    }
                }, new Action() {
                    @Override
                    public void run() throws Exception {
                        //onComplete
                        KLog.i("onComplete");
                        mView.closeProgressDialog();
                    }
                });
        mCompositeDisposable.add(disposable);
    }

    @Override
    public void quitGroup(Map map) {
        mView.showProgressDialog();
        Disposable disposable = httpAPIWrapper.quitGroup(map)
                .subscribe(new Consumer<BaseBack>() {
                    @Override
                    public void accept(BaseBack baseBack) throws Exception {
                        //isSuccesse
                        KLog.i("onSuccesse");
                        mView.closeProgressDialog();
                        mView.qiutGroupSuccess();
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        //onError
                        KLog.i("onError");
                        mView.closeProgressDialog();
                        throwable.printStackTrace();
                    }
                }, new Action() {
                    @Override
                    public void run() throws Exception {
                        //onComplete
                        KLog.i("onComplete");
                        mView.closeProgressDialog();
                    }
                });
        mCompositeDisposable.add(disposable);
    }
}